package cn.gongwebo.payview.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.gongwebo.payview.R;

/**
 * Created by GongWenBo on 2017/12/20.
 */

public class KeyBoardAdapter extends RecyclerView.Adapter<KeyBoardAdapter.KeyBoardViewHolder> {
    private List<Integer> mList;
    private Context       mContext;
    public static final int EMPTY  = -5;
    public static final int DELETE = -10;

    public KeyBoardAdapter(List<Integer> list, Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public KeyBoardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
        return new KeyBoardViewHolder(view);
    }

    @Override
    public void onBindViewHolder(KeyBoardViewHolder holder, int position) {
        final Integer code = mList.get(position);
        // 数字
        TextView tv = holder.tv;
        // 点击
        View view = holder.view;
        // 删除
        ImageView iv_delete = holder.iv_delete;
        iv_delete.setVisibility(code == DELETE ? View.VISIBLE : View.GONE);
        if (code == EMPTY) {
            view.setBackgroundResource(R.drawable.keyboard_empty);
        } else if (code == DELETE) {
            view.setBackgroundResource(R.drawable.keyboard_empty);
            iv_delete.setBackgroundResource(R.drawable.keyboard_delete);
        } else {
            view.setBackgroundResource(R.drawable.keyboard_num);
            tv.setText(mList.get(position) + "");
        }
        // 最右边的不显示
        if (holder.getPosition() % 3 == 2) {
            holder.view_right.setVisibility(View.GONE);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mKeyBoardAdapterListener != null) {
                    mKeyBoardAdapterListener.click(code);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class KeyBoardViewHolder extends RecyclerView.ViewHolder {

        TextView  tv;
        View      view;
        ImageView iv_delete;
        View      view_right;

        public KeyBoardViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            view = itemView.findViewById(R.id.rl);
            iv_delete = itemView.findViewById(R.id.iv_delete);
            view_right = itemView.findViewById(R.id.view_right);
        }
    }

    private KeyBoardAdapterListener mKeyBoardAdapterListener;

    public void setKeyBoardAdapterListener(KeyBoardAdapterListener keyBoardAdapterListener) {
        mKeyBoardAdapterListener = keyBoardAdapterListener;
    }

    public interface KeyBoardAdapterListener {
        void click(int code);
    }
}
